/* 
    输入：intervals = [[1,3],[2,6],[8,10],[15,18]]
    输出：[[1,6],[8,10],[15,18]]
    解释：区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

    作者：力扣 (LeetCode)
    链接：https://leetcode.cn/leetbook/read/array-and-string/c5tv3/
    来源：力扣（LeetCode）
    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处 
*/

let intervals = [
	[1, 3],
	[2, 6],
	[8, 10],
	[15, 18],
]
let s = intervals.reduce((prev, cur) => {
	let peek = prev[prev.length - 1] // prev[]
	if (peek && peek[1] >= cur[0]) {
		let left = peek[0]
		let right = peek[1] > cur[1] ? peek[1] : cur[1]
		prev[prev.length - 1] = [left, right]
	} else {
		prev.push(cur)
	}
	return prev
}, [])
console.log('s ', s)
